/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class Subject extends BaseDAO {
    private Connection con = getConnection();
    private ResultSet rs = null;
    private PreparedStatement pStmt = null;
    private Statement stmt = null;

    public ResultSet viewSubject() {
        try {
            stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM SUBJECT");
        } catch (SQLException ex) {
            Logger.getLogger(Subject.class.getName()).log(Level.SEVERE, null, ex);
        }

        return rs;
    }
    public ResultSet viewSubSubject(int id) {
        try {
            stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM SUBSUBJECT WHERE SubjectID = "+id);
        } catch (SQLException ex) {
            Logger.getLogger(Subject.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }
    public boolean deleteSubject(int id) {
        try {
            rs = this.viewSubSubject(id);
            if (rs.next()) {
                return false;
            } else {
                pStmt = con.prepareCall("DELETE FROM SUBJECT WHERE SubjectID = ?");
                pStmt.setInt(1, id);

                if(pStmt.executeUpdate() == 1 ) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(Subject.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public ResultSet viewQuestion(int id) {
        try {
            stmt = con.createStatement();
            rs = stmt.executeQuery("SELECT * FROM Questions WHERE SubID = "+id);
            rs = stmt.executeQuery("SELECT * FROM Test WHERE SubID = "+id);
        } catch (SQLException ex) {
            Logger.getLogger(Subject.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }
    public boolean deleteSubSubject(int id) {
        try {
            rs = this.viewQuestion(id);
            if (rs.next()) {
                return false;
            } else {
                pStmt = con.prepareCall("DELETE FROM SUBSUBJECT WHERE SubID = ?");
                pStmt.setInt(1, id);

                if(pStmt.executeUpdate() == 1 ) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(Subject.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}
